package cn.wolfcode.mq;

import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.mapper.OrderInfoMapper;
import cn.wolfcode.service.IOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        consumerGroup = "MQOrderTimeOutListener",
        topic = MQConstant.ORDER_PAY_TIMEOUT_TOPIC
)
@Slf4j
public class MQOrderTimeOutListener implements RocketMQListener<OrderMQResult> {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private IOrderInfoService orderInfoService;

    @Override
    public void onMessage(OrderMQResult orderMQResult) {
        log.info("超时取消消费者");
        // 1.判断订单是否超时
        OrderInfo orderInfo = orderInfoMapper.find(orderMQResult.getOrderNo());
        if (orderInfo != null) {
            // 2.如果是未支付状态
            if (OrderInfo.STATUS_ARREARAGE.equals(orderInfo.getStatus())) {
                orderInfoService.cancelOrder(orderMQResult);
            }
        }
        // 3.回补真实库存

        // 4.回补预库存

        // 5.修改本地标识

        // 6.修改订单状态

    }
}